package com.wcl.hencodertest.batteryMeterView;

import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
 * @项目名： HenCoderTest
 * @包名： com.wcl.hencodertest.batteryMeterView
 * @文件名: RectDrawable
 * @创建者: kilin
 * @创建时间: 2020/4/28 10:12
 * @描述： TODO
 */
public class RectDrawable extends Drawable {

    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private int mHeight;
    private int mWidth;


    @Override
    public void draw(@NonNull Canvas canvas) {
        canvas.save();
        canvas.translate((mWidth / 2F), (mHeight / 2F));
        canvas.rotate(-90);
        canvas.translate(-(mWidth/ 2F), -(mHeight / 2F));
        canvas.drawRect(mWidth / 2F - 50, mHeight / 2F - 100, mWidth / 2F + 50, mHeight / 2F + 100, paint);
        canvas.drawRect(mWidth / 2F - 20, mHeight / 2F - 120, mWidth / 2F +20, mHeight / 2F - 100, paint);
        canvas.restore();
    }

    @Override
    public void setBounds(int left, int top, int right, int bottom) {
        super.setBounds(left, top, right, bottom);
        mHeight = bottom - top;
        mWidth = right - left;
    }

    @Override
    public void setAlpha(int alpha) {
        paint.setAlpha(alpha);
    }

    @Override
    public int getAlpha() {
        return paint.getAlpha();
    }

    @Override
    public void setColorFilter(@Nullable ColorFilter colorFilter) {
        paint.setColorFilter(colorFilter);
    }

    @Override
    public int getOpacity() {
        return paint.getAlpha() == 0 ? PixelFormat.TRANSPARENT :
                paint.getAlpha() == 0xff ? PixelFormat.OPAQUE : PixelFormat.TRANSLUCENT;
    }
}
